package GDoc::Job::Fetch::US::Exec::Treas::FMS::AR;

use Moose;


extends 'GDoc::Job::Fetch';

sub setup {
    my $self = shift;
    $self->_baseurl( 'http://fms.treas.gov/annualreport/backissues.html' );
    $self->_basedir( 'http://fms.treas.gov/annualreport/' );
}

sub run { 
    my $self = shift;
    my %data = %{ $self->data };

    if ( $data{link} ) { 
        $self->_fetch_pdf;
    } elsif ( $data{index} ) { 
        $self->_fetch_index;
    } else { 
        $self->logger->info( "Fetching base " . $self->_baseurl );
        my $mech = $self->_mech;
        $mech->get( $self->_baseurl );

        my @rep_links = $mech->find_all_links( text_regex => qr{Annual Report for} );
        foreach my $rep( @rep_links ) { 
            my $txt = $rep->text;
            $txt =~ m{(?<year>\d{4})};
            my $year = $+{year};

            my $job = GDoc::Job->new( class => 'Fetch::US::Exec::Treas::FMS::AR',
                                      queue => $self->fetch_queue,
                                      data  => { link => $self->_basedir . $rep->url,
                                                 desc => $rep->text,
                                                 year => $year }
                                    );

            $job->submit;
        }

        my @idx_links = $mech->find_all_links( text_regex => qr{Appendix for} );
        foreach my $idx( @idx_links ) { 
            my $job = GDoc::Job->new( class => 'Fetch::US::Exec::Treas::FMS::AR',
                                      queue => $self->fetch_queue,
                                      data  => { index => $self->_basedir . $idx->url }
                                    );

            $job->submit;
        }
    }
}

sub _fetch_index { 
    my $self = shift;
    my %data = %{ $self->data };

    $self->logger->info( "fetching index page $data{index}" );
    my $mech = $self->_mech;
    $mech->get( $data{index} );
    
    $data{index} =~ m{(?<dir>[\w\:\./]+?/)index\.html};
    my $appdir = $+{dir};

    my $html = $mech->response->content;
    $html =~ m{Annual Report Appendix - (?<year>\d{4})};
    my $year = $+{year};

    die "Can't find year" unless $year;

    my $secnum = 0;
    while ( $html =~ m{<p>\s?(?<title>[\w\s\'-]+)</p>.*?<a href="[\w\./]*?(?<url>\w+.pdf)">}gsi ) { 
        my ( $title, $url ) = @+{'title', 'url'};
        $title =~ s/\n|\r//g;

        $url = $appdir . $url;
        my $job = GDoc::Job->new( class => 'Fetch::US::Exec::Treas::FMS::AR',
                                  queue => $self->fetch_queue,
                                  data  => { link    => $url,
                                             desc    => $title,
                                             year    => $year,
                                             secnum  => ++$secnum
                                           }
                                );

        $job->submit;
    }
}
    

sub _fetch_pdf { 
    my $self = shift;
    my %data = %{ $self->data };

    my ( $link, $desc, $year ) = @data{ 'link', 'desc', 'year' };
    my $secnum = $data{secnum} || '';

    my $ident = 'FMS-AR-';
    $ident .= $secnum
      ? sprintf( '%04d-%02d', $year, $secnum )
      : $year;
    
    my $filename = $ident . '.pdf';
    my $key = 'us/exec/treas/fms/' . $filename;

    $self->logger->info( "Storing $link --> $key" );

    my $mech = $self->_mech;
    $mech->get( $link );

    $self->store_pdf( key        => $key,
                      data       => $mech->response->content,
                      filename   => $filename,
                      meta       => { description => $desc,
                                      identifier  => $ident
                                    }
                    );
}


1;
